package mockproject.fresher15.sd.controller;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.Query;

import mockproject.fresher15.sd.model.Stock;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class DisplayDataController implements Serializable{

	private static final long serialVersionUID = 1L;
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/")
	public String getData(Model model){
		model.addAttribute("data","Data avvaiable");
		List<Stock> list=new LinkedList<Stock>();
		EntityManager ma=Persistence.createEntityManagerFactory("persistenceUnits").createEntityManager();
		ma.getTransaction().begin();
		Query q=ma.createQuery("select s from Stock s");
		list=q.getResultList();
		model.addAttribute("stocks", list);
		ma.getTransaction().commit();
		return "display";
	}
	@SuppressWarnings("unchecked")
	public static void main(String []a){
		try{
		List<Stock> list=new LinkedList<Stock>();
		EntityManager ma=Persistence.createEntityManagerFactory("persistenceUnits").createEntityManager();
		ma.getTransaction().begin();
		Query q=ma.createQuery("from Stock");
		list=q.getResultList();
		//model.addAttribute("stocks", list);
		ma.getTransaction().commit();
		System.out.println(list.get(0).getId());
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	
}
